var graceJS = require('@/utils/grace.js');
var PASSWORD = '1234';
var USERNAME = '223';
var ISLOGIN = '8868';
var storageName = 'jfsxety';

var storage = function(keyName, value) {
	var tmpVal = uni.getStorageSync(storageName);
	tmpVal = tmpVal ? JSON.parse(tmpVal) : {};
	if (keyName === storageName) {
		uni.setStorageSync(storageName, JSON.stringify(Object.assign({}, tmpVal, value)));
	} else {
		if (value) {
			tmpVal[keyName] = value;
			uni.setStorageSync(storageName, JSON.stringify(tmpVal));
		} else {
			if (graceJS.isEmptyObj(tmpVal)) {
				return
			}
			if (typeof keyName == 'string') {
				return tmpVal[keyName];
			}
			if (keyName instanceof Array) {
				var v = tmpVal
				for (var i = 0; i < keyName.length; i++) {
					if (typeof v[keyName[i]] == 'undefined') {
						v = '';
					} else {
						v = v[keyName[i]]
					}
				}
				return v;
			}
		}
	}
}

// 全局业务状态
module.exports = {
	isLogin: (status) => {
		var state = '';
		if (typeof status !== 'undefined') {
			state = status;
			storage(ISLOGIN, status)
		} else {
			return state || storage(ISLOGIN)
		}
	}, // 是否已登录 使用界面 compute 实实获取 false 未登录 true 已登录
	username: (username) => {
		var name = '';
		if (username) {
			name = username;
			storage(USERNAME, username)
		} else {
			return name || storage(USERNAME)
		}
	}, // 登录账号
	password: (password) => {
		var name = '';
		if (password) {
			name = password;
			storage(PASSWORD, password)
		} else {
			return name || storage(PASSWORD)
		}
	}, // 登录密码
	loginOut: () => {
		graceJS.clearStorage();
	},
	storage,
	storageName,
	apiUrl: 'https://sydwtestpc.qmgczx.cn',
	isDebug: false
}